package com.ag.utils.param.verify;

import com.ag.exception.InvalidParamException;
import com.ag.utils.NumberUtils;
import com.ag.utils.param.IVerifyAndConvertValue;

public class NumberInvalidVerifyAndConvertValue implements IVerifyAndConvertValue {
	@Override
	public Object getValue(Object value, String viewName, Object defaultValue) {
		if(value == null) {
			throw new InvalidParamException(viewName + "值非法");
		}
		
		if(!(value instanceof Number)) {
			throw new ClassCastException("数值区间验证值应该是Number类型,而不是" + value.getClass().getSimpleName() + "类型");
		}
		
		boolean isValid = NumberUtils.isValid((Number) value);
		if(!isValid) {
			if(defaultValue != null) {
				return defaultValue;
			}
			
			throw new InvalidParamException(viewName + "值非法");
		}
		return value;
	}
}
